//
//  MeHeaderView.m
//  PigMarket
//
//  Created by Felix on 2019/3/22.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "MeHeaderView.h"

#import "UIView+Add.h"

@interface MeHeaderView ()

/**
 headerImageView
 */
@property (nonatomic , strong) UIImageView *headerImageView;
/**
 nameLabel
 */
@property (nonatomic , strong) UILabel *nameLabel;
/**
 用户类型
 */
@property (nonatomic , strong) UIImageView *userType;

/**
 locationIcon
 */
@property (nonatomic , strong) UIImageView *locationIcon;
/**
 address
 */
@property (nonatomic , strong) UILabel *address;
/**
 age
 */
@property (nonatomic , strong) UILabel *age;
/**
 性别
 */
@property (nonatomic , strong) UIImageView *gender;
/**
 remark
 */
@property (nonatomic , strong) UILabel *remark;

@end

@implementation MeHeaderView

#pragma mark - init method
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self addSubview:self.bottomView];
    [self addSubview:self.headerImageView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.userType];
    [self.bottomView addSubview:self.locationIcon];
    [self.bottomView addSubview:self.address];
    [self.bottomView addSubview:self.age];
    [self.bottomView addSubview:self.gender];
    [self.bottomView addSubview:self.remark];
}

- (void)initLayout {
    // 布局子控件
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(58);
    }];
    [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(26);
        make.centerY.mas_equalTo(self.bottomView.mas_top);
        make.size.mas_equalTo(CGSizeMake(76, 76));
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headerImageView.mas_right).offset(6);
        make.bottom.mas_equalTo(self.bottomView.mas_top).offset(-4);
        make.height.mas_lessThanOrEqualTo(28);
    }];
    [self.userType mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel.mas_right).offset(4);
        make.centerY.mas_equalTo(self.nameLabel.mas_centerY);
        make.right.mas_lessThanOrEqualTo(-20);
    }];
    [self.nameLabel setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    [self.userType setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.locationIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel.mas_left);
        make.top.mas_equalTo(4);
        make.size.mas_equalTo(CGSizeMake(14, 14));
    }];
    [self.address mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.locationIcon.mas_right).offset(4);
        make.centerY.mas_equalTo(self.locationIcon.mas_centerY);
        make.width.mas_lessThanOrEqualTo(100);
    }];
    [self.address setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.age mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.address.mas_right).offset(4);
        make.centerY.mas_equalTo(self.locationIcon.mas_centerY);
    }];
    [self.age setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.gender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.age.mas_right).offset(4);
        make.centerY.mas_equalTo(self.locationIcon.mas_centerY);
    }];
    [self.remark mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel.mas_left);
        make.top.mas_equalTo(self.locationIcon.mas_bottom).offset(4);
        make.right.mas_equalTo(-5);
    }];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    [self.bottomView setupRoundedRect:self.bottomView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(8, 8)];
}

#pragma mark - getter and setter
- (void)setUserDetailModel:(UserDetailInfo *)userDetailModel {
    _userDetailModel = userDetailModel;
    [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:userDetailModel.headImg] placeholderImage:[UIImage imageNamed:@"placeholder_small"]];
    self.nameLabel.text = userDetailModel.nickName;
    self.address.text = userDetailModel.regionName;
    self.age.text = [NSString stringWithFormat:@"%@岁",userDetailModel.age];
    self.remark.text = [JTool iSNull:userDetailModel.personality] ? @"编辑你的个性签名" : userDetailModel.personality ;
    if ([userDetailModel.sex intValue] == 1) {//男
        self.gender.image = [UIImage imageNamed:@"user_man"];
    }else if ([userDetailModel.sex intValue] == 2) {//女
        self.gender.image = [UIImage imageNamed:@"user_woman"];
    }
    if ([userDetailModel.identity intValue] == 1) {//会员
        self.userType.image = [UIImage imageNamed:@"register_selectInviter_putongyuan"];

    }else if ([userDetailModel.identity intValue] == 2) {//服务商
        self.userType.image = [UIImage imageNamed:@"register_selectInviter_siyangyuan"];
    }
}


//聊天查看个人资料
- (void)setChatUserInfoModel:(ChatUserInfo *)chatUserInfoModel {
    _chatUserInfoModel = chatUserInfoModel;
    [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:chatUserInfoModel.headImg] placeholderImage:[UIImage imageNamed:@"placeholder_header"]];
    self.nameLabel.text = chatUserInfoModel.nickName;
    self.address.text = chatUserInfoModel.nowPosition;
    self.age.text = [NSString stringWithFormat:@"%@岁",chatUserInfoModel.age];
    self.remark.text = [JTool iSNull:chatUserInfoModel.personality] ? @"暂无个性签名" : chatUserInfoModel.personality ;
    if ([chatUserInfoModel.sex intValue] == 1) {//男
        self.gender.image = [UIImage imageNamed:@"user_man"];
    }else if ([chatUserInfoModel.sex intValue] == 2) {//女
        self.gender.image = [UIImage imageNamed:@"user_woman"];
    }
    if ([chatUserInfoModel.identity intValue] == 1) {//会员
        self.userType.image = [UIImage imageNamed:@"register_selectInviter_putongyuan"];
        
    }else if ([chatUserInfoModel.identity intValue] == 2) {//服务商
        self.userType.image = [UIImage imageNamed:@"register_selectInviter_siyangyuan"];
    }
}


- (UIImageView *)headerImageView {
    if (!_headerImageView) {
        _headerImageView = [[UIImageView alloc] init];
        _headerImageView.layer.cornerRadius = 38;
        _headerImageView.layer.masksToBounds = YES;
    }
    return _headerImageView;
}
- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
        _nameLabel.font = [UIFont xxLargerFont];
    }
    return _nameLabel;
}
- (UIImageView *)userType {
    if (!_userType) {
        _userType = [[UIImageView alloc] init];
    }
    return _userType;
}
- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = [UIColor whiteColor];
    }
    return _bottomView;
}
- (UIImageView *)locationIcon {
    if (!_locationIcon) {
        _locationIcon = [[UIImageView alloc] init];
        _locationIcon.image = [UIImage imageNamed:@"user_location"];
    }
    return _locationIcon;
}
- (UILabel *)address {
    if (!_address) {
        _address = [[UILabel alloc] init];
        _address.textColor = [UIColor colorWithHexString:@"#707070"];
        _address.font = [UIFont smallFont];
    }
    return _address;
}
- (UILabel *)age {
    if (!_age) {
        _age = [[UILabel alloc] init];
        _age.textColor = [UIColor colorWithHexString:@"#707070"];
        _age.font = [UIFont smallFont];
    }
    return _age;
}
- (UIImageView *)gender {
    if (!_gender) {
        _gender = [[UIImageView alloc] init];
    }
    return _gender;
}
- (UILabel *)remark {
    if (!_remark) {
        _remark = [[UILabel alloc] init];
        _remark.textColor = [UIColor colorWithHexString:@"#707070"];
        _remark.font = [UIFont smallFont];
    }
    return _remark;
}

@end
